<?php

namespace app\weixin\controller;

//宝箱
class Box extends Base{

    public function _initialize(){
        parent::_initialize();
    }

    public function index(){
        $map = [];
        $pagesize = config('paginate')['list_rows'];//每页数量
        $param=request()->param(); //获取url参数
        $lists = db('box')->where($map)->order('id desc')->paginate($pagesize,false,array('query' => array_splice($param,1)));
        $prop_type = config('prop_type');
        $list = array();
        foreach ($lists as $key => $value) {
            $_t = $value;
            $list[] = $_t;
        }  
        foreach ($list as $key => &$val) {
            $temps= explode(',', $val['reward_prop_type']);
            if (!empty($temps)) {
                $str=[];
                foreach ($temps as $k => $tmp) {
                    $str[]=!empty($prop_type[$tmp])?$prop_type[$tmp]:'';
                }
                $val['reward_prop_type']= implode(',',$str);
            }
        }
        $this->assign("list",$list);
        $this->assign("page", $lists->render());
        return $this->fetch();
    }

    //添加宝箱
    public function add_box(){
        $prop_type = config('prop_type');
        if(request()->isPost()){
            $data = input('post.');
            $reward_prop_type = implode($data['reward_prop_type'], ',');
            $data['reward_prop_type'] = $reward_prop_type;
            $data['create_time'] = time();
            $res = db('box')->insertGetId($data);
            return $this->success('操作成功');
        }
        $this->assign("prop_type",$prop_type);
        return $this->fetch();
    }

    //修改
    public function edit_box(){
        $id = input('id');
        $prop_type_string = config('prop_type');
        if(request()->isPost()){
            $data = input('post.');
            if(!empty($data['reward_prop_type'])){
                $reward_prop_type = implode($data['reward_prop_type'], ',');
                $data['reward_prop_type'] = $reward_prop_type;
            }else{
                $data['reward_prop_type'] = '';
            }
            $res = db('box')->where(['id'=>$id])->update($data);
            return $this->success('操作成功');
        }
        $data = db('box')->where(['id'=>$id])->find();
        $prop_type_num = explode(",", $data['reward_prop_type']);
        $this->assign('data',$data);
        $this->assign("prop_type_string",$prop_type_string);
        $this->assign("prop_type_num",$prop_type_num);
        return $this->fetch();
    }

    //删除
    public function del_box(){
        $id = input('id');
        $res = db('box')->where(['id'=>$id])->delete();
        return $this->success('操作成功');
    }

    //设置状态
    public function box_state(){
        $id=input('id');
        $status=input('status');
        $res = db('box')->where(['id'=>$id])->setField(['status'=>$status]);
        return $this->success('操作成功');
    }  

} 